package com.peng.leetcode.list;

/**
 * RemoveElements
 *
 * @author: lupeng6
 * @create: 2020/12/31 14:22
 */
public class RemoveElements {

    public static void main(String[] args) {
        ListNode listNode = BuildListNode.tailInsert(1, 2, 3, 4, 5);
        PrintListNode.print(listNode);
        ListNode listNode1 = removeElements(listNode, 1);
        PrintListNode.print(listNode1);
    }

    public static ListNode removeElements(ListNode head, int val) {
        if (head == null) {
            return null;
        }
        ListNode sentinel = new ListNode(-1);
        sentinel.next = head;
        ListNode prev = sentinel;
        ListNode current = head;
        while (current != null) {
            if (current.val == val) {
                prev.next = current.next;
            } else {
                prev = current;
            }
            current = current.next;
        }
        return sentinel.next;
    }
}
